package com.my.xq.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.my.utils.IdValidator;
import com.my.utils.MobilePhoneValidator;
import com.my.xq.domain.entity.Court;
import com.my.xq.domain.entity.Doorplate;
import com.my.xq.domain.entity.Proprietor;
import com.my.xq.repository.CourtRepository;
import com.my.xq.repository.DoorplateRepository;
import com.my.xq.repository.ProprietorRepository;
import com.my.xq.service.impl.CourtServiceImpl;
import com.sskj.common.util.CalendarUtil;
import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.core.exception.MException;
import com.sskj.sys.domain.entity.MediaEntity;
import com.sskj.sys.repository.MediaRepository;

@RestController
@RequestMapping("/excel")
public class ExcelImportConstroller {

	@Value("${sskj.conf.fileUploadPath}")
	private String fileUploadPath;

	@Autowired
	CourtRepository courtRepository;

	@Autowired
	DoorplateRepository doorplateRepository;

	@Autowired
	ProprietorRepository proprietorRepository;

	@Autowired
	MediaRepository mediaRepository;

	@Autowired
	CourtServiceImpl courtService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "导入Excel", log = false)
	@RequestMapping(value = "/importExcels", method = { RequestMethod.POST })
	public Map<String, Object> importExcels(Doorplate p) {
		File tempFile = new File(p.getFilePath());
		Map<String, Object> map = new HashMap<String, Object>();
		InputStream is = null;
		try {
			is = new FileInputStream(tempFile);
			Workbook wb = this.basicOperate(is, p.getFilesuffix());
			// 根据excel里面的内容读取知识库信息
			// 得到第一个shell
			Sheet sheet = wb.getSheetAt(0);
			// 得到Excel的行数
			int totalRows = sheet.getPhysicalNumberOfRows();
			// 总列数
			int totalCells = 0;
			// 得到Excel的列数(前提是有行数)，从第二行算起
			if (totalRows >= 2 && sheet.getRow(1) != null) {
				totalCells = sheet.getRow(1).getPhysicalNumberOfCells();
			}
			this.readDoorPlateExcelValue(wb, p.getCourtId(), totalRows, totalCells, sheet, map);

			// 删除上传的临时文件
			File tempFiledir = new File(this.getUpGradeDirPath(p.getFilePath()));
			if (tempFiledir.exists()) {
				this.deleteDir(tempFiledir);
			}
			// 删除数据库的文件信息
			mediaRepository.delete(p.getFileid());
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", 2);
			map.put("errorMsg", e);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					is = null;
					e.printStackTrace();
				}
			}
		}
		return map;
	}

	@RequestMapping(value = "/upload", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "小区数据导入")
	public Map<String, Object> uploadExcel(@RequestParam MultipartFile file) throws MException {
		InputStream is = null;
		try {
			is = file.getInputStream();
			String filename = file.getOriginalFilename();
			Workbook wb = this.basicOperate(is, filename.substring(filename.lastIndexOf(".") + 1));
			Map<String, Object> excelCeckResutl = this.checkUploadExcel(wb);
			if ("0".equals(excelCeckResutl.get("error").toString())) {
				Map<String, Object> result = new HashMap<String, Object>();

				String path = CalendarUtil.getTodayYymmdd() + "/" + UUID.randomUUID().toString() + "/";

				File targetFile = new File(fileUploadPath + path);
				if (!targetFile.exists()) {
					targetFile.mkdirs();
				}
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(fileUploadPath + path + filename));
				MediaEntity wme = new MediaEntity();
				wme.setMedianame(filename);
				wme.setMediasuffix(filename.substring(filename.lastIndexOf(".") + 1));
				wme.setHttpurl("upload/" + path + filename);
				wme.setMediasize(file.getSize());
				wme.setMediaurl(fileUploadPath + path + filename);
				wme.setAssociatedentity("MediaEntity");
				mediaRepository.save(wme);
				result.put("error", 0);
				result.put("obj", wme);
				return result;
			} else {
				return excelCeckResutl;
			}
		} catch (IOException ioe1) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("error", 4);
			map.put("errorMsg", ioe1.getMessage());
			return map;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ioe2) {
					is = null;
					ioe2.printStackTrace();
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("error", 4);
					map.put("errorMsg", ioe2.getMessage());
					return map;
				}
			}
		}

	}

	private Map<String, Object> checkUploadExcel(Workbook wb) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 根据excel里面的内容读取知识库信息
		int numberOfSheets = wb.getNumberOfSheets();
		if (numberOfSheets == 0) {
			map.put("error", 1);
			map.put("errorMsg", "Excel文件为空");
			return map;
		}
		// 得到第一个shell
		Sheet sheet = wb.getSheetAt(0);
		// 得到Excel的行数
		int totalRows = sheet.getPhysicalNumberOfRows();
		if (totalRows == 0) {
			map.put("error", 1);
			map.put("errorMsg", "Excel文件为空");
			return map;
		}

		if (totalRows < 11) {
			map.put("error", 1);
			map.put("errorMsg", "Excel文件行数不够");
			return map;
		}

		// 获取小区名称
		String courtName = this.getCellValueString(sheet.getRow(1), 3);
		if (StringUtils.isEmpty(courtName)) {
			map.put("error", 1);
			map.put("errorMsg", "名称不能为空");
			return map;
		}

		// 获取小区总户数
		Double doorCount = this.getCellValueNumeric(sheet.getRow(2), 3);
		if (doorCount == null) {
			map.put("error", 1);
			map.put("errorMsg", "总户数不能为空");
			return map;
		}

		// 获取小区所在经度
		Double lng = this.getCellValueNumeric(sheet.getRow(3), 3);
		if (lng == null) {
			map.put("error", 1);
			map.put("errorMsg", "经度不能为空");
			return map;
		}

		// 获取小区所在纬度
		Double lat = this.getCellValueNumeric(sheet.getRow(4), 3);
		if (lat == null) {
			map.put("error", 1);
			map.put("errorMsg", "纬度不能为空");
			return map;
		}

		// 获取小区详细地址
		String address = this.getCellValueString(sheet.getRow(5), 3);
		if (StringUtils.isEmpty(address)) {
			map.put("error", 1);
			map.put("errorMsg", "地址不能为空");
			return map;
		}

		// 获取小区联系人
		String linkman = this.getCellValueString(sheet.getRow(6), 3);
		if (linkman != null && linkman.length() > 20) {
			map.put("error", 1);
			map.put("errorMsg", "字符太长（联系人不能超过20个字符，一个汉字等于两个字符）");
			return map;
		}

		// 获取小区联系电话
		String phoneno = this.getCellValueString(sheet.getRow(7), 3);
		if (phoneno != null && phoneno.length() > 20) {
			map.put("error", 1);
			map.put("errorMsg", "字符过长（联系人电话不能超过20个字符）");
			return map;
		}

		for (int i = 10; i < totalRows; i++) {
			Row row = sheet.getRow(i);
			// 楼栋号
			Double building = this.getCellValueNumeric(row, 1);
			if (building == null) {
				map.put("error", 2);
				map.put("errorMsg", "楼栋号必须是数字");
				return map;
			}
			// 单元号
			Double unit = this.getCellValueNumeric(row, 2);
			if (unit == null) {
				map.put("error", 2);
				map.put("errorMsg", "单元号必须是数字");
				return map;
			}
			// 楼层
			Double floor = this.getCellValueNumeric(row, 3);
			if (floor == null) {
				map.put("error", 2);
				map.put("errorMsg", "楼层号必须是数字");
				return map;
			}
			// 房号
			Double doorNo = this.getCellValueNumeric(row, 4);
			if (doorNo == null) {
				map.put("error", 2);
				map.put("errorMsg", "房号必须是数字");
				return map;
			}
			// 门牌号
			String doorplateNo = this.getCellValueString(row, 5);
			if (StringUtils.isEmpty(doorplateNo) || doorplateNo.length() > 40) {
				map.put("error", 2);
				map.put("errorMsg", "门牌号不能为空且长度不能超过40字符");
				return map;
			}

			// 业主姓名
			String pIdentityCard = this.getCellValueString(row, 9);
			if (StringUtils.isNotBlank(pIdentityCard)) {

				if (!IdValidator.isValidatedAllIdcard(pIdentityCard)) {
					map.put("error", 3);
					map.put("errorMsg", "业主身份证号码（" + pIdentityCard + "）不对");
					return map;
				}

				// 业主姓名
				String pName = this.getCellValueString(row, 6);
				if (StringUtils.isBlank(pName) || pName.length() > 40) {
					map.put("error", 3);
					map.put("errorMsg", "（身份证号码为：" + pIdentityCard + "）的业主姓名不能为空且长度不能超过40字符");
					return map;
				}
				// 业主性别
				String pGenger = this.getCellValueString(row, 7);
				if (!"男".equals(pGenger) && !"女".equals(pGenger)) {
					map.put("error", 3);
					map.put("errorMsg", "（身份证号码为：" + pIdentityCard + "）的业主性别必须为男或女");
					return map;
				}
				// 业主电话
				String pPhone = this.getCellValueString(row, 8);
				if (!MobilePhoneValidator.isMobileExact(pPhone)) {
					map.put("error", 3);
					map.put("errorMsg", "（身份证号码为：" + pIdentityCard + "）的业主手机号码不对");
					return map;
				}
			}
		}
		map.put("error", 0);
		map.put("successMsg", "文件格式数据验证通过！");
		return map;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "导入Excel", log = false)
	@RequestMapping(value = "/import/door", method = { RequestMethod.POST })
	public Map<String, Object> uploadExcelCourtData(String courtId, String filePath, String fileSuffix, String fileId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Court court = null;
		if (StringUtils.isNotBlank(courtId)) {
			court = courtRepository.get(courtId);
			if (court == null) {
				map.put("error", 1);
				map.put("errorMsg", "系统错误，您选择的小区不存在，导入数据失败！");
				return map;
			}
		}
		InputStream is = null;
		try {
			if (filePath == null || fileSuffix == null) {
				map.put("error", 1);
				map.put("errorMsg", "系统错误，文件不存在，导入数据失败！");
				return map;
			}
			is = new FileInputStream(new File(filePath));
			Workbook wb = this.basicOperate(is, fileSuffix);
			// 根据excel里面的内容读取知识库信息
			int numberOfSheets = wb.getNumberOfSheets();
			if (numberOfSheets == 0) {
				map.put("error", 2);
				map.put("errorMsg", "Excel文件为空，导入数据失败！");
				return map;
			}
			// 得到第一个shell
			Sheet sheet = wb.getSheetAt(0);
			// 得到Excel的行数
			int totalRows = sheet.getPhysicalNumberOfRows();
			if (totalRows == 0) {
				map.put("error", 2);
				map.put("errorMsg", "Excel文件为空，导入数据失败！");
				return map;
			}

			if (totalRows < 11) {
				map.put("error", 2);
				map.put("errorMsg", "Excel文件行数不够，导入数据失败！");
				return map;
			}

			// 获取小区名称
			String courtName = this.getCellValueString(sheet.getRow(1), 3);
			if (StringUtils.isEmpty(courtName)) {
				map.put("error", 2);
				map.put("errorMsg", "小区名称不能为空，导入数据失败！");
				return map;
			}

			if (court == null) {
				court = courtRepository.findByName(courtName);
			} else {
				if (!courtName.equals(court.getName())) {
					map.put("error", 2);
					map.put("errorMsg", "导入的数据与您选择的小区不匹配（小区名称必须一致），导入数据失败！");
					return map;
				}
			}

			if (court == null) {
				court = new Court();
				court.setName(courtName);
			} 

			// 获取小区总户数
			Double doorCount = this.getCellValueNumeric(sheet.getRow(2), 3);
			if (doorCount == null) {
				map.put("error", 2);
				map.put("errorMsg", "总户数不能为空，导入数据失败！");
				return map;
			}
			court.setCourtcount(doorCount.intValue());

			// 获取小区所在经度
			Double lng = this.getCellValueNumeric(sheet.getRow(3), 3);
			if (lng == null) {
				map.put("error", 2);
				map.put("errorMsg", "经度不能为空，导入数据失败！");
				return map;
			}
			court.setLng(lng);

			// 获取小区所在纬度
			Double lat = this.getCellValueNumeric(sheet.getRow(4), 3);
			if (lat == null) {
				map.put("error", 2);
				map.put("errorMsg", "纬度不能为空，导入数据失败！");
				return map;
			}
			court.setLat(lat);

			// 获取小区详细地址
			String address = this.getCellValueString(sheet.getRow(5), 3);
			if (StringUtils.isEmpty(address)) {
				map.put("error", 2);
				map.put("errorMsg", "地址不能为空，导入数据失败！");
				return map;
			}
			court.setAddress(address);

			// 获取小区联系人
			String linkman = this.getCellValueString(sheet.getRow(6), 3);
			if (linkman != null && linkman.length() > 20) {
				map.put("error", 2);
				map.put("errorMsg", "字符太长（联系人不能超过20个字符，一个汉字等于两个字符），导入数据失败！");
				return map;
			}
			court.setLinkman(linkman);

			// 获取小区联系电话
			String phoneno = this.getCellValueString(sheet.getRow(7), 3);
			if (phoneno != null && phoneno.length() > 20) {
				map.put("error", 2);
				map.put("errorMsg", "字符过长（联系人电话不能超过20个字符），导入数据失败！");
				return map;
			}
			court.setPhoneno(phoneno);
			courtRepository.save(court);
			courtService.addRenWu(court.getId());

			List<Doorplate> doors = new ArrayList<Doorplate>();
			List<Proprietor> ps = new ArrayList<Proprietor>();
			for (int i = 10; i < totalRows + 2; i++) {
				Row row = sheet.getRow(i);

				// 业主身份证
				String pIdentityCard = this.getCellValueString(row, 9);

				Proprietor p = null;
				if (StringUtils.isNotBlank(pIdentityCard)) {

					if (!IdValidator.isValidatedAllIdcard(pIdentityCard)) {
						map.put("error", 2);
						map.put("errorMsg", "业主身份证号码（" + pIdentityCard + "）不对，导入数据失败！");
						return map;
					}

					p = proprietorRepository.findByCert(pIdentityCard);
					if (p == null) {
						p = new Proprietor();
					}

					// 业主姓名
					String pName = this.getCellValueString(row, 6);
					if (StringUtils.isBlank(pName) || pName.length() > 40) {
						map.put("error", 2);
						map.put("errorMsg", "（身份证号码为：" + pIdentityCard + "）的业主姓名不能为空且长度不能超过40字符，导入数据失败！");
						return map;
					}
					// 业主性别
					String pGenger = this.getCellValueString(row, 7);
					if (!"男".equals(pGenger) && !"女".equals(pGenger)) {
						map.put("error", 2);
						map.put("errorMsg", "（身份证号码为：" + pIdentityCard + "）的业主性别必须为男或女，导入数据失败！");
						return map;
					}
					// 业主电话
					String pPhone = this.getCellValueString(row, 8);
					if (!MobilePhoneValidator.isMobileExact(pPhone)) {
						map.put("error", 2);
						map.put("errorMsg", "（身份证号码为：" + pIdentityCard + "）的业主手机号码不对，导入数据失败！");
						return map;
					}

					p.setIdentityCard(pIdentityCard);
					p.setName(pName);
					p.setGender("男".equals(pGenger) ? 1 : 2);
					p.setPhone(pPhone);
					proprietorRepository.save(p);
					ps.add(p);
				}

				// 楼栋号
				String building = this.getCellValueString(row, 1);
				if (building == null) {
					map.put("error", 2);
					map.put("errorMsg", "楼栋号不能为空，导入数据失败！");
					return map;
				}
				// 单元号
				String unit = this.getCellValueString(row, 2);
				if (unit == null) {
					map.put("error", 2);
					map.put("errorMsg", "单元号不能为空，导入数据失败！");
					return map;
				}
				// 楼层
				String floor = this.getCellValueString(row, 3);
				if (floor == null) {
					map.put("error", 2);
					map.put("errorMsg", "楼层号不能为空，导入数据失败！");
					return map;
				}
				// 房号
				String no = this.getCellValueString(row, 4);
				if (no == null) {
					map.put("error", 2);
					map.put("errorMsg", "房号不能为空，导入数据失败！");
					return map;
				}
				// 门牌号
				String doorplateNo = this.getCellValueString(row, 5);
				if (StringUtils.isEmpty(doorplateNo) || doorplateNo.length() > 40) {
					map.put("error", 2);
					map.put("errorMsg", "门牌号不能为空且长度不能超过40字符，导入数据失败！");
					return map;
				}

				Doorplate door = doorplateRepository.findByBuildingAndUnit(court.getId(), building, unit, floor, no);
				if (door == null) {
					door = new Doorplate();
					door.setCourtId(court.getId());
					door.setCourtName(court.getName());
					door.setBuilding(building);
					door.setUnit(unit);
					door.setFloor(floor);
					door.setNo(no);
					door.setDoorplateNo(doorplateNo);
				}
				if (p != null) {
					door.setProprietorId(p.getId());
					door.setOpenId(p.getOpenid());
				}
				doorplateRepository.save(door);
				doors.add(door);
			}

			logger.info("成功导入房屋数据" + doors.size() + "条；业主数据"+ ps.size() +"条。");
			map.put("error", 0);
			map.put("successMsg", "导入数据成功！");

		} catch (Exception e) {
			map.put("error", 3);
			map.put("errorMsg", e.getMessage());
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					is = null;
					map.put("error", 3);
					map.put("errorMsg", e.getMessage());
				}
			}
		}
		return map;
	}

	private String getCellValueString(Row row, int cellNumber) {
		if (row == null) {
			return null;
		}
		int numberOfCells = row.getPhysicalNumberOfCells();
		if (numberOfCells >= cellNumber) {
			Cell cell = row.getCell(cellNumber);
			if (cell != null) {
				if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
					Double value = cell.getNumericCellValue();
					return String.valueOf(value.intValue());
				} else {
					return cell.getStringCellValue();
				}
			}
		}
		return null;
	}

	private Double getCellValueNumeric(Row row, int cellNumber) {
		if (row == null) {
			return null;
		}
		int numberOfCells = row.getPhysicalNumberOfCells();
		if (numberOfCells >= cellNumber) {
			Cell cell = row.getCell(cellNumber);
			if (cell != null) {
				int type = cell.getCellType();
				try {
					if (type == 0) {
						return cell.getNumericCellValue();
					} else if (type == 1) {
						return Double.valueOf(cell.getStringCellValue());
					} else {
						return null;
					}
				} catch (NumberFormatException e) {
					logger.debug(e.getMessage());
				}
			}
		}
		return null;
	}

	public Workbook basicOperate(InputStream is, String filesuffix) throws IOException {
		// 根据版本选择创建Workbook的方式
		Workbook wb = null;
		// 根据文件名判断文件是2003版本还是2007版本
		if ("xlsx".equals(filesuffix)) {
			wb = new XSSFWorkbook(is);
		} else {
			wb = new HSSFWorkbook(is);
		}
		return wb;
	}

	/**
	 * 解析Excel里面的数据
	 * 
	 * @param wb
	 * @return
	 */
	private void readDoorPlateExcelValue(Workbook wb, String courtId, int totalRows, int totalCells, Sheet sheet,
			Map<String, Object> map) {
		String errorMsg = "";
		String successMsg = "";
		List<Doorplate> doorPlateList = new ArrayList<Doorplate>();
		Doorplate doorplate;

		String br = "<br/>";

		// 循环Excel行数,从第二行开始。标题不入库
		for (int r = 1; r < totalRows; r++) {
			String rowMessage = "";
			Row row = sheet.getRow(r);
			if (row == null) {
				errorMsg += br + "第" + (r + 1) + "行数据有问题，请仔细检查！";
				continue;
			}
			doorplate = new Doorplate();

			String doorplateNo = "";
			String unit = "";
			String building = "";

			// 循环Excel的列
			for (int c = 0; c < totalCells; c++) {
				Cell cell = row.getCell(c);
				if (null != cell) {
					if (c == 0) {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						doorplateNo = cell.getStringCellValue();
						if (this.isEmpty(doorplateNo)) {
							rowMessage += "门牌号不能为空";
						} else if (doorplateNo.length() > 40) {
							rowMessage += "门牌号的长度不能超过40；";
						}
						doorplate.setDoorplateNo(doorplateNo);
					}
					if (c == 1) {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						unit = cell.getStringCellValue();
						if (this.isEmpty(unit)) {
							rowMessage += "单元号不能为空；";
						} else if (unit.length() > 40) {
							rowMessage += "单元号的长度不能超过40；";
						}
						doorplate.setUnit(unit);
					}
					if (c == 2) {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						building = cell.getStringCellValue();
						if (this.isEmpty(building)) {
							rowMessage += "楼栋号不能为空；";
						} else if (building.length() > 40) {
							rowMessage += "楼栋号的长度不能超过40；";
						}
						doorplate.setBuilding(building);
					}
				} else {
					rowMessage += "第" + (c + 1) + "列数据有问题，请仔细检查；";
				}
			}
			// 拼接每行的错误提示
			if (!this.isEmpty(rowMessage)) {
				errorMsg += br + "第" + (r + 1) + "行，" + rowMessage;
			} else {
				doorplate.setCourtId(courtId);
				doorPlateList.add(doorplate);
			}
		}

		// 全部验证通过才导入到数据库
		if (this.isEmpty(errorMsg)) {
			for (Doorplate d : doorPlateList) {
				doorplateRepository.save(d);
			}
			successMsg = "导入成功，共" + doorPlateList.size() + "条数据！";
			map.put("result", 1);
			map.put("successMsg", successMsg);
		} else {
			map.put("result", 2);
			map.put("errorMsg", errorMsg);
		}
	}

	public List<String> toList(String value, String separator) {
		List<String> result = new ArrayList<String>();
		if (!this.isEmpty(value)) {
			String[] as = value.split(separator);
			for (int i = 0; i < as.length; i++) {
				result.add(as[i]);
			}
		}
		return result;
	}

	public boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public String getUpGradeDirPath(String path) {
		String a = path.substring(0, path.lastIndexOf("/"));
		return a;
	}

	public boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		return dir.delete();
	}
}
